<template>
  <div class="app-container">
    <!-- 查询模块 -->
    <div class="formDiv">
      <el-form ref="form"  :inline="true" :model="form" label-width="130px">
        <el-row>
          <el-col :span="6">
            <el-form-item label="姓名">
              <el-input v-model="form.name"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="手机号">
              <el-input v-model="form.userName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="所属公司">
              <el-select placeholder="请选择" v-model="form.officeId">
                <el-option
                  v-for="item in officeOption"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="地址">
              <el-cascader placeholder="请选择省市" :options="options" v-model="areas"></el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-button style="margin-left:60px" type="primary" @click="searchFn">查询</el-button>
            </el-form-item>
            <el-form-item>
              <el-button
                v-if="hasPermission('AdminButlerProvider.createButler')"
                type="primary"
                @click="toRoutes('stewardItemAdd')"
              >新增</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 主页列表 -->
    <div class="listDiv" v-loading="tableLoading">
      <!-- table列表 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column fixed width="100px" prop="provinceCity" label="地区"></el-table-column>
        <el-table-column prop="name" width="100px" label="管家姓名"></el-table-column>
        <el-table-column prop="userName" width="120px" label="管家手机号">
           <template slot-scope="scope">
                <div>{{scope.row.userName | phonereg }}</div>
              </template>
        </el-table-column>
        <el-table-column prop="officeName" width="120px" label="绑定公司"></el-table-column>
        <el-table-column prop="hospitalNameList" label="已绑定医院"></el-table-column>
        <el-table-column prop="createdAt" width="100px" label="添加时间"></el-table-column>
        <el-table-column width="60px" label="状态">
          <!-- <template slot-scope="scope">
                        {{scope.row.isFreeze}}
          </template>-->
          <template slot-scope="scope">
            <el-switch
              class="switchStyle"
              v-model="scope.row.isFreeze"
              @click.native="changeStatus('serverAppointmentStatus',scope.row.isFreeze,scope.row.id)"
              active-color="#13ce66"
              inactive-color="#eee"
              :active-value="1"
              :inactive-value="2"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="180">
          <template slot-scope="scope">
            <el-button
              v-if="hasPermission('AdminButlerProvider.bindButlerHospital')"
              type="text"
              size="small"
              @click="toRoutes('stewardItemHospital',scope.row.id)"
            >绑定医院</el-button>
            <el-button
              v-if="hasPermission('AdminButlerProvider.updateButler')"
              type="text"
              @click="toRoutes('stewardItemEdit',scope.row.id)"
              size="small"
            >编辑</el-button>
            <el-button
              type="text"
              v-if="hasPermission('AdminButlerProvider.butlerLogs')"
              @click="opendia(scope.row.id)"
              size="small"
            >日志</el-button>
            <el-button
              v-if="hasPermission('AdminButlerProvider.deleteButler')"
              type="text"
              @click="deleteList(scope.row.id)"
              size="small"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pagesCon">
        <div>
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="listForm.pageNum"
            :page-sizes="page_sizes"
            :page-size="listForm.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page_totals"
          ></el-pagination>
        </div>
      </div>
    </div>
    <el-dialog title="操作日志" :visible.sync="dialogVisible" width="30%">
      <div>
        <el-table :data="rowData" style="width: 100%">
          <el-table-column type="index" label="序号" align="center" :index="table_index" width="50"></el-table-column>
          <el-table-column prop="description" align="center" label="操作说明"></el-table-column>
          <el-table-column prop align="center" label="操作人">
            <template slot-scope="scope">
              <span>{{scope.row.adminId | userinfo}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="createdDate" align="center" label="操作时间"></el-table-column>
        </el-table>
        <el-pagination
          @size-change="SizeChange"
          @current-change="CurrentChange"
          :current-page="params.pageNum"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="params.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import filter from "@/assets/filter.js";
import {
  butlerList,
  deleteButler,
  freezeButler,
  butlerLogs
} from "@/api/stewardItem";
import { companyList } from "@/api/branchOfficeDropDown";
import { regionDropDown } from "@/api/area";
export default {
  name: "stewardItemIndex",
  data() {
    return {
      areas: [],
      dialogVisible: false,
      tableLoading: true,
      dates: [],
      rowData: [],
      form: {
        provinceId: null,
        cityId: null,
        pageNum: 1,
        pageSize: 10
      },
      options: [],
      tableData: [],
      page_sizes: [10, 30, 50, 100],
      page_totals: 60,
      total: 0,
      listForm: {
        pageNum: 1,
        pageSize: 10
      },
      officeOption: [],
      params: {
        pageNum: 1,
        pageSize: 10,
        id: 0
      }
    };
  },
  filters: {
    cardType: function(e) {
      return window.helper.cardType(String(e));
    },
    isFreezeFn(e) {
      if (e == 1) {
        return "正常";
      } else {
        return "冻结";
      }
    }
  },
  inject: ["reload"],
  mounted() {
    this.getList(this.listForm);
    this.getOption();
  },
  methods: {
    async list() {
      let res = await butlerLogs(this.params);
      console.log(res);
      if (res.data.code == 0) {
        this.rowData = res.data.data.responseDTOS;
        this.total = res.data.data.count;
      }
    },
    SizeChange(val) {
      this.params.pageNum = 1;
      this.params.pageSize = val;
      this.list();
    },
    CurrentChange(val) {
      this.params.pageNum = val;
      this.list();
    },
    table_index(index) {
      return (this.params.pageNum - 1) * this.params.pageSize + index + 1;
    },
    opendia(id) {
      this.params.id = id;
      this.dialogVisible = true;
      this.params.pageNum = 1;
      this.list();
    },
    //按钮权限
    hasPermission(e) {
      return window.helper.hasPermission(e);
    },
    //获取列表
    getOption() {
      //分公司列表
      companyList()
        .then(res => {
          let resData = res.data.data;
          let newData = [];
          resData.forEach(e => {
            newData.push({
              value: e.id,
              label: e.name
            });
          });
          this.officeOption = newData;
        })
        .catch(error => {
          this.$message.error(error);
        });
      // 省市列表
      regionDropDown().then(res => {
        let newArr = [];
        res.data.data.forEach((item1, index) => {
          newArr.push({
            value: item1.id,
            label: item1.name
          });
          if (item1.children != null) {
            newArr[index]["children"] = [];
            item1.children.forEach(item2 => {
              newArr[index]["children"].push({
                value: item2.id,
                label: item2.name
              });
            });
          }
        });
        this.options = newArr;
      });
    },
    //成功状态弹窗
    succStatus(txt) {
      this.$message({
        message: txt + "成功",
        type: "success"
      });
    },
    //失败状态弹窗
    errorStatus(txt) {
      this.$message.error(txt + "失败");
    },
    //列表数据获取
    getList(e) {
      this.tableLoading = true;
      let datas = e;
      butlerList(datas)
        .then(res => {
          // console.log(res)
          if (!res.data.code) {
            // console.log(res)
            this.tableData = res.data.data.list;
            this.page_totals = res.data.data.count;
          } else {
            this.$message.error(res.data.msg);
          }
          this.tableLoading = false;
        })
        .catch(error => {
          this.$message.error(error);
        });
    },
    // 查询
    searchFn() {
      this.form.pageNum = 1;
      if (this.areas.length > 0) {
        this.form.provinceId = this.areas[0];
        this.form.cityId = this.areas[1];
      }
      this.getList(this.form);
      // console.log(JSON.stringify(this.form))
    },

    addSubmit() {
      console.log("提交了");
      this.dialogVisible = false;
    },
    closeMake(e) {
      console.log(e);
      this.dialogVisible = true;
    },
    // 路由跳转
    toRoutes(e, userId) {
      let route = { name: e, query: { userId: userId } };
      this.$router.push(route);
    },
    //改变页面条数
    handleSizeChange(val) {
      this.form.pageSize = val;
      this.getList(this.form);
    },
    //上下架与状态修改
    changeStatus(type, status, id) {
      console.log(1111, status);
      let datas = {
        isFreeze: status,
        id: id
      };
      let fonts = "";
      if (datas.isFreeze == 1) {
        console.log();
        fonts = "启用";
      } else {
        fonts = "冻结";
      }
      console.log(2222, fonts);
      this.$confirm("是否确认" + fonts + "此管家", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          freezeButler(datas)
            .then(res => {
              // console.log(res)
              if (res.data.code === 0) {
                this.$message({
                  message: "修改成功",
                  type: "success"
                });
              } else {
                this.$message.error("状态切换失败");
              }
              this.reload();
            })
            .catch(error => {
              this.$message.error(error);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消状态切换"
          });
          this.reload();
        });
    },
    //选择页数
    handleCurrentChange(val) {
      this.form.pageNum = val;
      this.getList(this.form);
    },
    // 删除
    deleteList(e) {
      let datas = { id: e };
      // console.log(datas)
      this.$confirm("是否确认删除此管家信息？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          // console.log(datas)
          deleteButler(datas).then(res => {
            if (!res.data.code) {
              this.succStatus("删除");
              this.getList(this.form);
              this.reload();
            } else {
              this.$message.error(res.data.msg);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  }
};
</script>
<style scoped>
/* 分页 */
.pagesCon {
  height: 40px;
  margin-top: 20px;
}
.pagesCon > div {
  float: right;
}
/* 表单样式修改 */
.el-range-editor--medium.el-input__inner {
  width: 236px;
}
</style>
